using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
    public class ServiceProviderPoolManager
    {        
        /// <summary>
        /// Add entry to PoolServiceProvider table
        /// </summary>
        /// <param name="providerID"></param>
        /// <param name="poolID"></param>
        /// <returns></returns>
        public static bool ServiceProviderPoolInsert(HSDPrincipal currentPrincipal, string providerID, int poolID)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string content = string.Format("<{0}>{1}</{0}><{2}>{3}</{2}>", "PoolID", poolID, "ServiceProviderID", providerID);
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderPoolInsert", content);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.ServiceProviderPoolInsert), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

        /// <summary>
        /// Deletes a list of ServiceProviders from a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolsptLinks"></param>
        /// <returns></returns>
        public static bool ServiceProviderPoolsLinksDelete(HSDPrincipal currentPrincipal, ServiceProviderPoolLinks serviceProviderPoolLinks)
        {
            string serviceProviderPoolLinksXml = AdapterHelper.ConstructObjectXml(serviceProviderPoolLinks, typeof(ServiceProviderPoolLinks));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, serviceProviderPoolLinksXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.ServiceProviderPoolsDelete), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }

        /// <summary>
        /// Adds a list of ServiceProviders to a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolsptLinks"></param>
        /// <returns></returns>
        public static bool ServiceProviderPoolsLinksInsert(HSDPrincipal currentPrincipal, ServiceProviderPoolLinks serviceProviderPoolLinks)
        {
            string serviceProviderPoolLinksXml = AdapterHelper.ConstructObjectXml(serviceProviderPoolLinks, typeof(ServiceProviderPoolLinks));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, serviceProviderPoolLinksXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.ServiceProviderPoolsInsert), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }



        /// <summary>
        /// Remove entry from PoolServiceProvider table
        /// </summary>
        /// <param name="providerID"></param>
        /// <param name="poolID"></param>
        /// <returns></returns>
        public static bool ServiceProviderPoolDelete(HSDPrincipal currentPrincipal, string providerID, int poolID)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string content = string.Format("<{0}>{1}</{0}><{2}>{3}</{2}>", "PoolID", poolID, "ServiceProviderID", providerID);
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderPoolDelete", content);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                AdapterHelper.GetMessageTypeValue(MessageTypes.ServiceProviderPoolDelete), ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }
    }
}
